package com.ynet.middleground.riskmanage.mq.producer;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

import com.ynet.core.log.IfpLogger;
import com.ynet.middleground.riskmanage.mq.Source;

/**
 * 测试 MQ 专用
 * 
 * @author liulx
 */
@EnableBinding({Source.class})
@Component
public class SourceProducer {

    @Autowired
    private Source source;

    /**
     * 测试专用，往 MQ 推送消息
     * 
     * @param msg 推送消息内容
     */
    public void sendMessages(String msg) {
        IfpLogger.info("mq", "MQ 测试，往Binding-{}推送信息-{}", Source.O_RISKMANAGE, msg);
        String payload = msg;
        Map<String, Object> headers = new HashMap<>(1);
        // tags 标签对应 yml 配置文件
        headers.put("TAGS", "tag_test");
        MessageHeaders messageHeaders = new MessageHeaders(headers);
        Message<String> message = MessageBuilder.createMessage(payload, messageHeaders);
        this.source.notice().send(message);
    }

    /**
     * 测试发送入池信息专用，往 MQ 推送消息
     *
     * @param data 推送消息内容
     */
    public void sendMessagesBlchIn(Map<String, Object> data) {
        IfpLogger.info("mq", "模拟qqjy MQ 推送入池数据，往 Binding-{} 推送信息-{}", Source.OUT_RISKMANAGE, data);
        Map<String, Object> headers = new HashMap<>(1);
        // tags 标签对应 yml 配置文件
        headers.put("TAGS", "in");
        MessageHeaders messageHeaders = new MessageHeaders(headers);
        Message<Map<String, Object>> message = MessageBuilder.createMessage(data, messageHeaders);
        this.source.noticeBlch().send(message);
    }

    /**
     * 测试发送入池信息专用，往 MQ 推送消息
     *
     * @param data 推送消息内容
     */
    public void sendMessagesBlchOut(Map<String, Object> data) {
        IfpLogger.info("mq", "模拟qqjy MQ 推送出池数据 往 Binding-{} 推送信息-{}", Source.OUT_RISKMANAGE, data);
        Map<String, Object> headers = new HashMap<>(1);
        // tags 标签对应 yml 配置文件
        headers.put("TAGS", "out");
        MessageHeaders messageHeaders = new MessageHeaders(headers);
        Message<Map<String, Object>> message = MessageBuilder.createMessage(data, messageHeaders);
        this.source.noticeBlch().send(message);
    }

}
